package com.xuecheng.order.mq;

import com.xuecheng.framework.domain.task.XcTask;
import com.xuecheng.order.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

@Component
public class ChooseCourseTask {

    @Autowired
    TaskService taskService;

    @Scheduled(cron = "0/5 * * * * *")
    //定时发送任务消息到MQ
    public void sendTask(){
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        calendar.set(GregorianCalendar.MINUTE,-1); // 一分钟前
        Date time = calendar.getTime();
       // 查询前n条任务
        List<XcTask> list = taskService.findXctaskList(10, time);
        if (list.size() > 0 ){
            for (XcTask xcTask : list) {
              // service发送任务
                String ex = xcTask.getMqExchange();
                String routingKey = xcTask.getMqRoutingkey();
                taskService.publish(ex,routingKey,xcTask);
            }
        }

    }


}
